package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.ElmOrders;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.ElmOrdersMapper;
import com.robotserver.orders.param.ElmOrderSyncParam;
import com.robotserver.orders.param.ElmOrderSyncParamV3;
import com.robotserver.orders.param.biz.ElmOrderPullParam;
import com.robotserver.orders.service.ElmOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: lucher
 * @Date: 2023/03/16 09:42
 * @Description:
 */
@RestController
public class ElmOrderController {

    private static Logger logger = LoggerFactory.getLogger(ElmOrderController.class);

    @Autowired
    private ElmOrderService elmOrderService;
    @Autowired
    private ElmOrdersMapper elmOrdersMapper;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("elmorder/pull")
    public Object pullOrder(ElmOrderPullParam param){
        return elmOrderService.getOrderList(param);
    }

    /**
     * 本地下载(新版临时添加)
     * @param param
     * @return
     */
    @PostMapping("elmorder/pullNew")
    public Object pullOrderNew(ElmOrderPullParam param){
        return elmOrderService.getOrderListNew(param);
    }

    /**
     * 远程下载
     * @param elmOrderSyncParam
     * @return
     */
    @PostMapping("elmorder/remotePull")
    public Object remotePull(ElmOrderSyncParam elmOrderSyncParam){

        logger.info("--2.0手动远程下载饿了么订单--elmOrderRefundSyncParam={}", JSONObject.toJSONString(elmOrderSyncParam));

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(elmOrderSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+elmOrderSyncParam.getBegin_time()+"::"+elmOrderSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        elmOrderService.syncOrder_byAppKey_producer(elmOrderSyncParam);
        return true;
    }

    /**
     * 远程下载所有
     * @param elmOrderSyncParam
     * @return
     */
    @PostMapping("elmorder/remote15Pull")
    public Object remote15Pull(ElmOrderSyncParam elmOrderSyncParam){

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(elmOrderSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+elmOrderSyncParam.getBegin_time()+"::"+elmOrderSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        elmOrderService.syncOrder_byAppKey_producer(elmOrderSyncParam);
        return true;
    }

    /**
     * 根据父订单号获取订单
     * @param parentOrderId
     * @return
     */
    @PostMapping("elmorder/getByOrderId")
    public Object getByOrderId(String parentOrderId){
        return elmOrderService.getByParentOrderId(parentOrderId);
    }

    /**
     * 根据父子单号获取订单
     * @return
     */
    @PostMapping("elmorder/getByParAndCliId")
    public Object getByParAndCliId(String parent_order_no, String child_order_no){
        ElmOrders elmOrders = elmOrderService.getByParentOrderId(parent_order_no,child_order_no);
        return elmOrders;
    }

    /**
     * 远程下载-3.0V
     * @param elmOrderSyncParamV3
     * @return
     */
    @PostMapping("elmorder/remotePullV3")
    public Object remotePullV3(ElmOrderSyncParamV3 elmOrderSyncParamV3){

        logger.info("--3.0手动远程下载饿了么订单--elmOrderSyncParamV3={}", JSONObject.toJSONString(elmOrderSyncParamV3));


        //判断是否订单号为空，如果不为空，则先修改数据库此订单的最新修改时间
        if(StringUtils.isNotBlank(elmOrderSyncParamV3.getChildOrderNo())){
            elmOrdersMapper.updateUpdateTime(elmOrderSyncParamV3.getChildOrderNo());
        }

        if (elmOrderSyncParamV3 == null
                || elmOrderSyncParamV3.getBeginTime() == null
                || elmOrderSyncParamV3.getEndTime() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(elmOrderSyncParamV3.getBeginTime() < (Long.valueOf(now)-7776000)){
            logger.error("------远程拉取饿了么订单V3版本错误，开始时间不能小于90天前。-------" + "::" + elmOrderSyncParamV3.getBeginTime() + "::" + elmOrderSyncParamV3.getEndTime());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        ElmOrderSyncParam elmOrderSyncParam = new ElmOrderSyncParam();
        elmOrderSyncParam.setBegin_time(elmOrderSyncParamV3.getBeginTime());
        elmOrderSyncParam.setEnd_time(elmOrderSyncParamV3.getEndTime());
        if(elmOrderSyncParamV3.getQueryType() == 1){ //1：创建时间
            elmOrderSyncParam.setDownTimeType(2);
        }else if(elmOrderSyncParamV3.getQueryType() == 2){ //2:付款时间
            elmOrderSyncParam.setDownTimeType(1); //1-付款时间
        }else if(elmOrderSyncParamV3.getQueryType() == 3){ //3:结算时间
            elmOrderSyncParam.setDownTimeType(3); //3-结算时间
        }else{//更新时间
            elmOrderSyncParam.setDownTimeType(4); //更新时间
        }

        elmOrderService.syncOrder_byAppKey_producer(elmOrderSyncParam);

        return true;
    }
}
